package com.netty.reactot.reactorthread;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class MultHander implements Runnable{
    SocketChannel socketChannel;
    private Executor executor=Executors.newCachedThreadPool();
    public MultHander(SocketChannel channel) {
        this.socketChannel=channel;
    }

    @Override
    public void run() {
        executor.execute(new ReadHander( socketChannel));
    }

    static class ReadHander implements Runnable{
        SocketChannel socketChannel;
        ReadHander(SocketChannel socketChannel){
            this.socketChannel=socketChannel;
        }
        @Override
        public void run() {
            ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
            System.out.println("创建数组");
            String message="";
            int length;
            int total;
            try {
                /*do{
                     length = socketChannel.read(byteBuffer);
                     message+=new String(byteBuffer.array());
                }while (length>byteBuffer.capacity());
                System.out.println("发送的消息为："+message);*/
                do {
                    length = socketChannel.read(byteBuffer);
                    message+= new String(byteBuffer.array());
                    System.out.println(length);
                    // 就是判断数据是否有没有读完
                }while (length > byteBuffer.capacity());
                System.out.println(message);
                /**
                 * do {
                 *                     length = socketChannel.read(byteBuffer);
                 *                     message+= new String(byteBuffer.array());
                 *                     System.out.println(length);
                 *                     // 就是判断数据是否有没有读完
                 *                 }while (length > byteBuffer.capacity());
                 *                 System.out.println(message);
                 */
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
